package com.approval.approvalapp.center.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.HysListBean;
import com.approval.approvalapp.index.cygn.hys.HysShenqingActivity;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;

public class CommonDialogHelper {


    private static CommonDialogHelper instance;
    private Dialog viewpagerDialog;

    public static CommonDialogHelper getInstance() {
        if (instance == null) {
            instance = new CommonDialogHelper();
        }
        return instance;
    }

    private CommonDialogHelper() {
    }

    /***
     * 获取一个dialog
     *
     * @param context
     * @return
     */
    public AlertDialog.Builder getDialog(Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        return builder;
    }

    /**
     * @param context 上下文
     * @return
     */
    public void getCommonDialog(final Context context, final String title, final String id , String type, HysListBean.ListBean listBean) {
        final AlertDialog.Builder builder = getDialog(context);

        View rootView = LinearLayout.inflate(context, R.layout.common_dialog_view, null);
        ViewHolder viewHolder = new ViewHolder(rootView);
        builder.setView(rootView);
        final Dialog dialog = setDialogConfig(context, builder);

        viewHolder.tvTitle.setText(title);
        viewHolder.tvType.setText(type);
        if (type.equals("通过")) {
            viewHolder.tvType.setTextColor(Color.parseColor("#DE463A"));
            viewHolder.tvTimeStart.setTextColor(Color.parseColor("#DE463A"));
            viewHolder.tvTimeEnd.setTextColor(Color.parseColor("#DE463A"));
        } else {
            viewHolder.tvType.setTextColor(Color.parseColor("#1465FD"));
            viewHolder.tvTimeStart.setTextColor(Color.parseColor("#1465FD"));
            viewHolder.tvTimeEnd.setTextColor(Color.parseColor("#1465FD"));
        }
        viewHolder.tvTime.setText(listBean.getD_Data());
        viewHolder.tvWeek.setText(listBean.getZj());
        viewHolder.tvLocation.setText(listBean.getOrgname());
        String imStartTime = listBean.getImStartTime();
        if (TextUtils.isEmpty(imStartTime)) {
            viewHolder.tvTimeStart.setText("开始时间：");
            viewHolder.tvTimeEnd.setText("结束时间：");
        } else {
            viewHolder.tvTimeStart.setText("开始时间：" + getTime(Long.valueOf(imStartTime) * 1000));
            viewHolder.tvTimeEnd.setText("结束时间：" + getTime(Long.valueOf(listBean.getImEndTime()) * 1000));
        }
        viewHolder.more_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HysShenqingActivity.class);
                intent.putExtra("id", id + "");
                intent.putExtra("imPlaceN", title);

                context.startActivity(intent);
            }
        });
                dialog.show();
    }


    @NonNull
    private Dialog setDialogConfig(Context context, AlertDialog.Builder builder) {
        final Dialog dialog = builder.show();
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        if (context instanceof Activity) {
            Window dialogWindow = dialog.getWindow();
            WindowManager m = ((Activity) context).getWindowManager();
            Display d = m.getDefaultDisplay(); // 获取屏幕宽、高度
            WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
//        p.height = (int) (d.getHeight()); // 高度设置为屏幕的0.6，根据实际情况调整
            p.width = (int) (d.getWidth() * 0.74); // 宽度设置为屏幕的0.65，根据实际情况调整
            dialogWindow.setAttributes(p);
        }
        return dialog;
    }


    public interface DialogCallBack {
        void onButtonConfirmClick();

        void onButtonCancleClick();
    }


    static class ViewHolder {
        @Bind(R.id.tv_title)
        TextView tvTitle;
        @Bind(R.id.tv_type)
        TextView tvType;
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.tv_week)
        TextView tvWeek;
        @Bind(R.id.tv_location)
        TextView tvLocation;
        @Bind(R.id.tv_time_start)
        TextView tvTimeStart;
        @Bind(R.id.tv_time_end)
        TextView tvTimeEnd;
        @Bind(R.id.more_ll)
        LinearLayout more_ll;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    private String getTime(long times) {
        Date date = new Date(times);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }
}
